// const path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');

var getHtmlConfig = function(name) {
  return{
    template  : './src/view/' + name + '.html',
    filename  : 'view/' + name + '.html',
    inject    : true,
    hash      : true,
    chunks    : ['common', name]
  }
}

var config = {
//   entry: './src/page/index/index.js',
    entry:{
        'common'    : ['./src/page/common/index.js', 'webpack-dev-server/client?http://localhost:8888'],
        'index'     : './src/page/index/index.js',
        'user-login': './src/page/user-login/user-login.js',
        'result'    : './src/page/result/result.js',
        'list'      : './src/page/list/list.js',
        'detail'    : './src/page/detail/detail.js',
        'cart'      : './src/page/cart/cart.js',
        'about'     : './src/page/about/about.js',
        'user-register'     : './src/page/user-register/user-register.js',
        'user-center'     : './src/page/user-center/user-center.js',
        'user-password-reset'     : './src/page/user-password-reset/user-password-reset.js',
        'user-password-update'     : './src/page/user-password-update/user-password-update.js',
        'order-list'     : './src/page/order-list/order-list.js',
        'order-detail'     : './src/page/order-detail/order-detail.js',
        'order-confirm'     : './src/page/order-confirm/order-confirm.js',
        'user-center-update'     : './src/page/user-center-update/user-center-update.js',
        'payment'     : './src/page/payment/payment.js'
      },
    output: {
      // path: path.resolve(__dirname, 'dist'),
      path: './dist',
      publicPath: '/dist/',
      filename: 'js/[name].js'
    },
    externals:{
      'jquery': 'window.jQuery'
    },
    module:{
      loaders:[
        { test: /\.css$/, loader: ExtractTextPlugin.extract("style-loader", "css-loader")},
        { test: /\.(gif|png|jpg|woff|svg|eot|ttf)\??.*$/, loader: 'url-loader?limit=100&name=resource/[name].[ext]'},
        { test: /\.string$/, loader: 'html-loader'}
      ]
    },
    resolve : {
      alias : {
          node_modules    : __dirname + '/node_modules',
          util            : __dirname + '/src/util',
          page            : __dirname + '/src/page',
          service         : __dirname + '/src/service',
          image           : __dirname + '/src/image'
      }
    },
    plugins:[
      new webpack.optimize.CommonsChunkPlugin({
        name: 'common',
        filename: 'js/base.js'
      }),
      new ExtractTextPlugin('css/[name].css'),
      new HtmlWebpackPlugin(getHtmlConfig('index')),
      new HtmlWebpackPlugin(getHtmlConfig('user-login')),
      new HtmlWebpackPlugin(getHtmlConfig('result')),
      new HtmlWebpackPlugin(getHtmlConfig('list')),
      new HtmlWebpackPlugin(getHtmlConfig('detail')),
      new HtmlWebpackPlugin(getHtmlConfig('cart')),
      new HtmlWebpackPlugin(getHtmlConfig('about')),
      new HtmlWebpackPlugin(getHtmlConfig('user-register')),
      new HtmlWebpackPlugin(getHtmlConfig('user-center')),
      new HtmlWebpackPlugin(getHtmlConfig('user-password-reset')),
      new HtmlWebpackPlugin(getHtmlConfig('user-password-update')),
      new HtmlWebpackPlugin(getHtmlConfig('order-list')),
      new HtmlWebpackPlugin(getHtmlConfig('order-detail')),
      new HtmlWebpackPlugin(getHtmlConfig('order-confirm')),
      new HtmlWebpackPlugin(getHtmlConfig('user-center-update')),
      new HtmlWebpackPlugin(getHtmlConfig('payment'))
    ],
  };

module.exports = config;